var $image = $('.img-container > img'),
    $dataX = $('#dataX'),
    $dataY = $('#dataY'),
    $dataHeight = $('#dataHeight'),
    $dataWidth = $('#dataWidth'),
    $dataRotate = $('#dataRotate'),
    options = {
        background: false,
        aspectRatio: 16 / 9,
        preview: '.img-preview',
        crop: function(data) {
            $dataX.val(Math.round(data.x));
            $dataY.val(Math.round(data.y));
            $dataHeight.val(Math.round(data.height));
            $dataWidth.val(Math.round(data.width));
            $dataRotate.val(Math.round(data.rotate));
        }
    };

$image.cropper(options);

//上传功能
$('.confile').click(function() {
    $('.upload-button input').trigger('click')
})

//图片替换
$('.upload-button input').on('change', function() {
    //获取用户选择的图片
    let aFiles = $(this)[0].files
    let imgUrl = URL.createObjectURL(aFiles[0])
    $image.cropper('destroy')
    $image.attr('src', imgUrl)
    $image.cropper(options);
})

//获取文章列表数据
let token = JSON.parse(localStorage.getItem('token'))
getAticList(token, function(data) {
    let newda = data.data
    newda.forEach(value => {
        let temp = `
        <option value="${value.Id}">${value.name}</option>
        `
        $('.city').append(temp)
    });
    layui.form.render()
})


//发布文章
layui.form.on('submit(send)', function(data) { //当前容器的全部表单字段，名值对形式：{name: value}
    console.log(data);
    let title = data.field.title
    let cate_id = data.field.cate_id
    let content = layedit.getContent(index)
    let state = '已发布'
    let cover = $image.cropper('getCroppedCanvas', { width: 250, height: 140 })

    cover.toBlob(function(blob) {
        let cover_img = blob
        let fd = new FormData()
        fd.append('title', title)
        fd.append('cate_id', cate_id)
        fd.append('content', content)
        fd.append('state', state)
        fd.append('cover_img', cover_img)
        addNewAtic(token, fd, function(data) {
            layer.msg(data.message)
            setTimeout(() => {
                $(window.parent.document).find('#search').addClass('layui-this')
                $(window.parent.document).find('#search').next().removeClass('layui-this')
                location.href = '/home/article/list.html'
            }, 2000);
        }, function(data) {
            console.log(data)
        })
    })
    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
});







//存为草稿
layui.form.on('submit(cg)', function(data) {

    let title = data.field.title
    let cate_id = data.field.cate_id
    let content = layedit.getContent(index)
    let state = '草稿'
    let cover = $image.cropper('getCroppedCanvas', { width: 250, height: 140 })
    cover.toBlob(function(blob) {
        let cover_img = blob
        let fd = new FormData()
        fd.append('title', title)
        fd.append('cate_id', cate_id)
        fd.append('content', content)
        fd.append('state', state)
        fd.append('cover_img', cover_img)
        addNewAtic(token, fd, function(data) {
            layer.msg(data.message)
            setTimeout(() => {
                location.href = '/home/article/list.html'
            }, 2000);
        }, function(data) {
            console.log(data)
        })
    })
    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
});